package com.qding.park.manager.app.queue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.qding.park.common.core.exceptions.IExceptionHandle;
import com.qding.park.manager.app.queue.handle.ClearTimeOutOrderHandle;
import com.qding.park.manager.app.queue.handle.ExpireNoticeHandle;
import com.qding.park.manager.app.queue.handle.UserInfoSyncHandle;
import com.qding.park.manager.app.queue.util.SpringContextUtil;
import com.qding.park.manager.pojo.queue.QueueMsg;

public class ParkAppApplication {
	
	private static final Log _log = LogFactory.getLog(ParkAppApplication.class);
	
	public static final SpringContextUtil ctxUtil = new SpringContextUtil(); 
	
	
	public static void main(String[] args) throws Exception {
		_log.info("start!!!!!!!!!");
		
		ClassPathXmlApplicationContext context;
		context = new ClassPathXmlApplicationContext(new String[] { "disconf.xml" ,"spring-context.xml"});
		
		ctxUtil.setApplicationContext(context);
		context.start();
		
		//异常处理接口
		IExceptionHandle  exceptionHandle=ctxUtil.getApplicationContext().getBean(IExceptionHandle.class);
		
		startThread();
		
//		/******测试**************/
//		QueueMsg msg=new QueueMsg();
//		ExpireNoticeHandle expireNoticeHandle = SpringContextUtil.getApplicationContext().getBean(ExpireNoticeHandle.class);
//		expireNoticeHandle.handle(msg);
//		/******测试**************/
		
		synchronized (ParkAppApplication.class) {
			while (true) {
				try {
					ParkAppApplication.class.wait();
				} catch (InterruptedException e) {
					_log.error("== synchronized error:", e);
					exceptionHandle.handle(e);
				}
			}
		}
		
	}
	
	
	/**
	 * @Description (启动任务)
	 */
	private static void startThread() {
		
		AppContext appContext=ctxUtil.getApplicationContext().getBean(AppContext.class);
		
		appContext.startApp();

//		threadPool.execute(new Runnable() {
//			public void run() {
//				try {
//					while (true) {
//						// 如果当前活动线程等于最大线程，那么不执行
//						if (threadPool.getActiveCount() < threadPool.getMaxPoolSize()) {
//							final NotifyTask task = tasks.poll();
//							if (task != null) {
//								threadPool.execute(new Runnable() {
//									public void run() {
//										System.out.println(threadPool.getActiveCount() + "---------");
//										tasks.remove(task);
//										task.run();
//									}
//								});
//							}
//						}
//					}
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
	}

}
